package com.qdh.service.impl;

import com.qdh.mapper.OrdersMapper;
import com.qdh.model.Orders;
import com.qdh.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public List<Orders> findAll() {
        return ordersMapper.findAll();
    }

    @Override
    public void insert(Orders orders) {
        ordersMapper.insertOrders(orders);
        String ordersId = orders.getId();
        List<Integer> travellerIds = orders.getTravellerId();
        for (Integer travellerId : travellerIds) {
            ordersMapper.insertOrderIdAndTravellerIdd(ordersId,travellerId);
        }

    }

    @Override
    public Orders findById(Integer id) {
        return ordersMapper.findById(id);
    }

    @Override
    public void updateOrders(Orders orders) {
        ordersMapper.updateOrders(orders);
    }

    @Override
    public void deleteOrders(Integer id) {
        if(ordersMapper.countOrderAndTraveller(id)==0){
            ordersMapper.deleteOrders(id);
        }
    }

}
